package com.guaniu.voip.ui.main;

import static com.guaniu.voip.sip.SipManager.SIP_REG_ONLINE;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.guaniu.voip.R;
import com.guaniu.voip.sip.Constant;
import com.guaniu.voip.sip.SipManager;
import com.guaniu.voip.utils.DialerUtils;
import com.guaniu.voip.utils.Utils;

/**
 * Created by Jay on 2015/8/28 0028.
 */
public class DialnumberPage extends Fragment {

    private InputMethodManager mInputMethodManager;
    private EditText mEtNumber;
    private String mMySipNum;//
    public DialnumberPage() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_dialer,container,false);

        mInputMethodManager = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);

        mMySipNum = SipManager.getInstance().getMyAccount();
        mEtNumber = (EditText)view.findViewById(R.id.screen_tab_dialer_editText_number);

        DialerUtils.setDialerTextButton(view, R.id.screen_tab_dialer_button_0, "0", "+", DialerUtils.TAG_0, mOnDialerClick);
        DialerUtils.setDialerTextButton(view, R.id.screen_tab_dialer_button_1, "1", "", DialerUtils.TAG_1, mOnDialerClick);
        DialerUtils.setDialerTextButton(view, R.id.screen_tab_dialer_button_2, "2", "ABC", DialerUtils.TAG_2, mOnDialerClick);
        DialerUtils.setDialerTextButton(view, R.id.screen_tab_dialer_button_3, "3", "DEF", DialerUtils.TAG_3, mOnDialerClick);
        DialerUtils.setDialerTextButton(view, R.id.screen_tab_dialer_button_4, "4", "GHI", DialerUtils.TAG_4, mOnDialerClick);
        DialerUtils.setDialerTextButton(view, R.id.screen_tab_dialer_button_5, "5", "JKL", DialerUtils.TAG_5, mOnDialerClick);
        DialerUtils.setDialerTextButton(view, R.id.screen_tab_dialer_button_6, "6", "MNO", DialerUtils.TAG_6, mOnDialerClick);
        DialerUtils.setDialerTextButton(view, R.id.screen_tab_dialer_button_7, "7", "PQRS", DialerUtils.TAG_7, mOnDialerClick);
        DialerUtils.setDialerTextButton(view, R.id.screen_tab_dialer_button_8, "8", "TUV", DialerUtils.TAG_8, mOnDialerClick);
        DialerUtils.setDialerTextButton(view, R.id.screen_tab_dialer_button_9, "9", "WXYZ", DialerUtils.TAG_9, mOnDialerClick);
        DialerUtils.setDialerTextButton(view, R.id.screen_tab_dialer_button_star, "*", "", DialerUtils.TAG_STAR, mOnDialerClick);
        DialerUtils.setDialerTextButton(view, R.id.screen_tab_dialer_button_sharp, "#", "", DialerUtils.TAG_SHARP, mOnDialerClick);

        DialerUtils.setDialerImageButton(view, R.id.screen_tab_dialer_button_audio, R.drawable.voice_call_48, DialerUtils.TAG_AUDIO_CALL, mOnDialerClick);
        DialerUtils.setDialerImageButton(view, R.id.screen_tab_dialer_button_video, R.drawable.visio_call_48, DialerUtils.TAG_VIDEO_CALL, mOnDialerClick);
        DialerUtils.setDialerImageButton(view, R.id.screen_tab_dialer_button_del, R.drawable.ic_input_delete_48, DialerUtils.TAG_DELETE, mOnDialerClick);

        mEtNumber.setInputType(InputType.TYPE_NULL);
        mEtNumber.setFocusable(false);
        mEtNumber.setFocusableInTouchMode(false);
        mEtNumber.addTextChangedListener(new TextWatcher(){
            public void afterTextChanged(Editable s) {}
            public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
        });

        view.findViewById(R.id.screen_tab_dialer_button_0).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                appendText("+");
                return true;
            }
        });
        return view;
    }


    private final View.OnClickListener mOnDialerClick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int tag = Integer.parseInt(v.getTag().toString());
            final String number = mEtNumber.getText().toString();
            if(tag == DialerUtils.TAG_CHAT){
            }
            else if(tag == DialerUtils.TAG_DELETE){
                final int selStart = mEtNumber.getSelectionStart();
                if(selStart >0){
                    final StringBuffer sb = new StringBuffer(number);
                    sb.delete(selStart-1, selStart);
                    mEtNumber.setText(sb.toString());
                    mEtNumber.setSelection(selStart-1);
                }
            }
            else if(tag == DialerUtils.TAG_AUDIO_CALL){
                if(!Utils.isSpace(number)){
                    makeCall(number, Constant.MEDIA_TYPE_AUDIO);
                    SipManager.getInstance().setOutGoingVideo(false);
                    mEtNumber.setText("");
                }
            }
            else if(tag == DialerUtils.TAG_VIDEO_CALL){
                if(!Utils.isSpace(number)){
                    SipManager.getInstance().setOutGoingVideo(true);
                    makeCall(number, Constant.MEDIA_TYPE_VIDEO);
                    mEtNumber.setText("");
                }
            }
            else{
                final String textToAppend = tag == DialerUtils.TAG_STAR ? "*" : (tag == DialerUtils.TAG_SHARP ? "#" : Integer.toString(tag));
                appendText(textToAppend);
            }
        }
    };

    private void makeCall(String number, int mediaType){
        if(Utils.isSpace(number))return;

        if (mMySipNum != null && mMySipNum.equals(number)){
            Toast.makeText(getContext(), getString(R.string.call_me_not), Toast.LENGTH_SHORT).show();
            return;
        }

        if (SipManager.getInstance().getRegState() != SIP_REG_ONLINE){
            //Log.d(TAG, "mRegisterManager.getRegisterStatus() :"+mRegisterManager.getCurrentRegisterStatus());
            Toast.makeText(getContext(), getString(R.string.no_network), Toast.LENGTH_SHORT).show();
            return;
        }

        Intent intent = new Intent(getContext(), TryingCallActivity.class);
        intent.putExtra(Constant.MEDIA_TYPE, mediaType);
        intent.putExtra(Constant.IS_MAKE_CALL, true);
        intent.putExtra(Constant.CALL_NUMBER, number);
        startActivity(intent);
    }

    private void appendText(String textToAppend){
        final int selStart = mEtNumber.getSelectionStart();
        final StringBuffer sb = new StringBuffer(mEtNumber.getText().toString());
        sb.insert(selStart, textToAppend);
        mEtNumber.setText(sb.toString());
        mEtNumber.setSelection(selStart+1);
    }
}
